package com.kbbywyclient.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.model.base.BaseModel;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.VerifyPhoneModule;
import com.kbbywyclient.presenter.VerifyPhonePresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.VerifyPhoneContract;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomToolBar;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by Jiangzx on 2017/9/8 10:45.
 */

public class VerifyPhoneActivity extends BaseActivity implements VerifyPhoneContract.View{
    public static final String NAME_S="name";//姓名
    public static final String CARD_NUMBER_S="cardNumber";//银行卡号
    public static final String CARD_CODE_S="cardCode";//银行编号
    public static final String ID_CARD_NUMBER_S="idCard";//身份证号
    public static final String BANK_NAME_S="bankName";//银行名
    public static final String CARD_TYPE_I="cardType";//银行卡类型
    public static final String BANK_MOBILE_S="bankMobile";//银行预留手机号
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.tv_phone_number_info)
    TextView tvPhoneNumberInfo;
    @BindView(R.id.et_verify_code)
    EditText etVerifyCode;
    @BindView(R.id.tv_send_code)
    Button btSendCode;
    @BindView(R.id.bt_complete)
    Button btComplete;

    private String name;//姓名
    private String cardNumber;//银行卡号码
    private String cardCode;//银行编号
    private String idCardNumber;//身份证号码
    private String bankName;//银行名字
    private int cardType;//卡类型 1储蓄卡 2信用卡
    private String phoneNumber;//手机号

    private int countDown;
    private Handler handler;

    @Inject
    VerifyPhonePresenter presenter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_verify_phone;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->handleGoBack());
    }

    @Override
    public void initData() {
        Intent intent=getIntent();
        name=intent.getStringExtra(NAME_S);
        cardNumber=intent.getStringExtra(CARD_NUMBER_S);
        cardCode=intent.getStringExtra(CARD_CODE_S);
        idCardNumber=intent.getStringExtra(ID_CARD_NUMBER_S);
        bankName=intent.getStringExtra(BANK_NAME_S);
        phoneNumber=intent.getStringExtra(BANK_MOBILE_S);
        cardType=intent.getIntExtra(CARD_TYPE_I,1);

        String str=getString(R.string.text_phone_number_info);
        String phoneShower=phoneNumber.substring(0,3)+"****"+phoneNumber.substring(7);
        tvPhoneNumberInfo.setText(String.format(str,phoneShower));

        handler=new Handler();
        presenter.sendSms(phoneNumber,Constants.GDX0204);
        btSendCode.setEnabled(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("银行卡绑定-验证手机号");
    }

    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            if(countDown>0){
                btSendCode.setEnabled(false);
                btSendCode.setText(countDown+"s后重发");
                countDown--;
                handler.postDelayed(this,1000L);
            }else{
                btSendCode.setText("获取验证码");
                btSendCode.setEnabled(true);
            }
        }
    };

    private void startCountDown(){
        countDown=59;
        handler.postDelayed(runnable,1000L);
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getVerifyPhoneComponent(new VerifyPhoneModule(this)).inject(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @OnTextChanged(R.id.et_verify_code)
    public void afterTextChange(Editable s){
        if(s.toString().trim().length()>0){
            btComplete.setEnabled(true);
        }else{
            btComplete.setEnabled(false);
        }
    }

    @OnClick({R.id.tv_send_code,R.id.bt_complete})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_send_code:
                //发送验证码
                btSendCode.setEnabled(false);
                presenter.sendSms(phoneNumber, Constants.GDX0204);
                break;
            case R.id.bt_complete:
                //完成
                String smmCode=etVerifyCode.getText().toString().trim();
                HashMap<String,String> strParams=new HashMap<>();
                strParams.put("trueName",name);
                strParams.put("bankAccount",cardNumber);
                strParams.put("IDCardNo",idCardNumber);
                strParams.put("bankName",bankName);
                strParams.put("bankMobile",phoneNumber);
                strParams.put("mobileCode",smmCode);
                strParams.put("bankCode",cardCode);
                if(!TextUtils.isEmpty(sessionId)){
                    strParams.put("sessionID",sessionId);
                }
                HashMap<String,Integer> intParams=new HashMap<>();
                intParams.put("salesID", PreUtils.getUser(VerifyPhoneActivity.this).getSalesID());
                intParams.put("cardType",cardType);
                presenter.bindCard(strParams,intParams);
                break;
        }
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    private String sessionId;
    @Override
    public void onSmsSuccess(String sessionId) {
        this.sessionId=sessionId;
        startCountDown();
    }

    @Override
    public void onSmsFailure() {
        btSendCode.setEnabled(true);
        btSendCode.setText("获取验证码");
    }

    @Override
    public void onBindCardSuccess(BaseModel model) {
        //绑卡成功
        ToastUtil.showShort("绑卡成功！");
        EventBus.getDefault().post(new Event.CardBindSuccess());
        finish();
    }

    CustomDialog cancelDialog;
    private void handleGoBack(){
        if(cancelDialog==null){
            cancelDialog=new CustomDialog(VerifyPhoneActivity.this,
                    R.style.Customdialog, "卡贝贝提示", "是否取消绑卡？", new CustomDialog.ButtonListener() {
                @Override
                public void left() {
                    cancelDialog.dismiss();
                    finish();
                }

                @Override
                public void right() {
                    cancelDialog.dismiss();
                }
            },"是","否");
        }
        if(!cancelDialog.isShowing()){
            cancelDialog.show();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            handleGoBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
